package com.frain.springcloud.controller;

import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.frain.springcloud.environment.BootEnviment;

@Controller
@RequestMapping("/demo")
public class DemoController {

    @Value("${server.port}")
    public int port;

    @RequestMapping("/getFind")
    @ResponseBody
    public String getDemo(HttpServletRequest request) {
        System.out.println("session=" + request.getSession().getId());
        System.out.println("thread=" + Thread.currentThread().getId());
        System.out.println("....getDemo..");
        return "success";
    }

    @RequestMapping("/home")
    @ResponseBody
    public String home() {
        System.out.println(BootEnviment.getFrom());
        return "Hello World";
    }

    /**
     * 
     * @Title: timeThreeFinish 
     * @Description: 需要执行3秒完成 
     * @param @return
     * @param @throws InterruptedException 参数说明 
     * @return String 返回类型 
     * @throws 
     * @author frain
     * @date 2018年8月6日 上午10:16:26
     */
    @RequestMapping("/timeThreeFinish")
    @SuppressWarnings({ "unchecked", "static-access" })
    @ResponseBody
    public String timeThreeFinish() throws InterruptedException {

        System.out.println("this  server port" + port + "  timeThreeFinish  method");
        Thread.currentThread().sleep(3000l);

        System.out.println("timeThreeFinish   3m  finish");

        return "timeThreeFinish";
    }

    /**
     * 
     * @Title: randomException 
     * @Description: 人品报错方法
     * @param @return
     * @param @throws Exception 参数说明 
     * @return String 返回类型 
     * @throws 
     * @author frain
     * @date 2018年8月6日 上午10:19:05
     */
    @RequestMapping("/randomException")
    @ResponseBody
    public String randomException() throws Exception {

        System.out.println("this  server port" + port + "  randomException  method");

        if (new Random().nextInt(1000) % 2 == 0) {
            throw new Exception("执行异常");
        }

        System.out.println("randomException finish");
        return "randomException";

    }

}
